package com.bubaiwantong.algorithms1.exchange;

/**
 * @author bubaiwantong
 * @date 2022/12/7 23:51
 * @description this is a class file created by bubaiwantong in 2022/12/7 23:51
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        BubbleSort sort = new BubbleSort();
        sort.print(arr);
        sort.sort(arr);
        sort.print(arr);
    }


    /**
     * 冒泡排序
     *
     * @param arr
     */
    public void sort(int arr[]) {
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }


    /**
     * 交换
     *
     * @param arr
     * @param i
     * @param j
     */
    public void swap(int arr[], int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    /**
     * 输出数组
     *
     * @param arr
     */
    public void print(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

}
